package com.xmall.order.service.impl.other;

import com.xmall.msg.queue.OrderModule;
import com.xmall.msg.base.MsgSourceType;
import com.xmall.msg.entity.order.*;
import com.xmall.define.request.pay.OrderPaySuccessRequest;
import com.xmall.order.define.constants.OrderProperties;
import com.xmall.order.service.other.IMsgService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MsgServiceImpl implements IMsgService {

    @Resource
    private OrderProperties orderProperties;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 创建订单消息
     */
    @Override
    public void sendOrderCreateMsg(Long orderId, List<Long> cartIds) {
        OrderCreateMsg msg = new OrderCreateMsg()
            .build(MsgSourceType.ORDER, OrderModule.OrderCreate.ORDER_CREATE_FANOUT_EXCHANGE, OrderModule.OrderCreate.ORDER_CREATE_ROUTING_KEY)
            .orderId(orderId).cartIds(cartIds);
        rabbitTemplate.convertAndSend(OrderModule.OrderCreate.ORDER_CREATE_FANOUT_EXCHANGE, OrderModule.OrderCreate.ORDER_CREATE_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 订单支付检查消息
     */
    @Override
    public void sendOrderPayCheckMsg(Long orderId) {
        OrderPayCheckMsg msg = new OrderPayCheckMsg().build(MsgSourceType.ORDER, OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderPayCheck.DELAY_ORDER_PAY_CHECK_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderPayCheck.DELAY_ORDER_PAY_CHECK_ROUTING_KEY, msg, (message -> {
            message.getMessageProperties().setExpiration(orderProperties.getPayCheckMilliseconds().toString());
            return message;
        }), new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 发送支付成功消息
     */
    @Override
    public void sendPaySuccessMsg(OrderPaySuccessRequest request) {
        OrderPaySuccessMsg msg = new OrderPaySuccessMsg().build(MsgSourceType.ORDER, OrderModule.OrderPaySuccess.ORDER_PAY_SUCCESS_FANOUT_EXCHANGE, OrderModule.OrderPaySuccess.ORDER_PAY_SUCCESS_QUEUE)
                .info(request.getOrderId(), request.getOrderSn(), request.getTradeNo(), request.getPayTime(), request.getPayType(), request.getPayPlatform());
        rabbitTemplate.convertAndSend(OrderModule.OrderPaySuccess.ORDER_PAY_SUCCESS_FANOUT_EXCHANGE, OrderModule.OrderPaySuccess.ORDER_PAY_SUCCESS_QUEUE, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 取消订单检查消息
     */
    @Override
    public void sendOrderCancelCheckMsg(Long orderId) {
        OrderCancelCheckMsg msg = new OrderCancelCheckMsg().build(MsgSourceType.ORDER, OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderCancelCheck.DELAY_ORDER_CANCEL_CHECK_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderCancelCheck.DELAY_ORDER_CANCEL_CHECK_ROUTING_KEY, msg, (message -> {
            message.getMessageProperties().setExpiration(orderProperties.getCancelCheckMilliseconds().toString());
            return message;
        }), new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 取消订单消息
     */
    @Override
    public void sendOrderCancelMsg(Long orderId) {
        OrderCancelMsg msg = new OrderCancelMsg().build(MsgSourceType.ORDER, OrderModule.OrderCancel.ORDER_CANCEL_FANOUT_EXCHANGE, OrderModule.OrderCancel.ORDER_CANCEL_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(OrderModule.OrderCancel.ORDER_CANCEL_FANOUT_EXCHANGE, OrderModule.OrderCancel.ORDER_CANCEL_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 退款消息
     */
    @Override
    public void sendOrderRefundMsg(Long refundLogId) {
        OrderRefundMsg msg = new OrderRefundMsg().build(MsgSourceType.ORDER, OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderRefund.ORDER_REFUND_ROUTING_KEY).refundLogId(refundLogId);
        rabbitTemplate.convertAndSend(OrderModule.CommonExchange.ORDER_DIRECT_EXCHANGE, OrderModule.OrderRefund.ORDER_REFUND_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }
}
